package com.iprojectmanager.views;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.iprojectmanager.managers.*;
import com.iprojectmanager.entities.*;
//import java.util.*;

/**
 *
 * @author Alucard
 */
public class VistaProyecto extends Vista
{
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        if(request.getAttribute("operacion") != null)
        {
            if(request.getAttribute("operacion").toString().compareTo("crear") == 0)
            {
                mainView(response.getWriter(),request);
            }else if(request.getAttribute("operacion").toString().compareTo("modificar") == 0)
            {
                changeView(response.getWriter(),request.getParameter("proyectos"),request);
            }else
            {
                //mainView(response.getWriter());
            }
        }
    }

    public void mainView(PrintWriter out,HttpServletRequest request)
    {
        HttpSession user = request.getSession();
        Administrador root = (Administrador)user.getAttribute("user");
        
            out.println(Utilities.getDocType());
            out.println("<HTML>" + Utilities.getHead("Administrador del Sistema"));
            out.println("<div id=\"container\">");
            out.println(Utilities.getIntro("Vista de Creacion de Proyectos",root));

            String form = 
                    "<div id=\"pageContent\">"+
                            this.getForm() +
                            Utilities.getFooter() + 
                    "</div>";

            out.println(form);
            out.println(Utilities.getMenu());
            out.println("</div> <!-- fin container---></BODY></HTML>");
            out.close();
            //<option selected label="Elija" value="no">Elija </option>
    }
    
    public void changeView(PrintWriter out,String proyecto,HttpServletRequest request)
    {
        HttpSession user = request.getSession();
        Administrador root = (Administrador)user.getAttribute("user");
        
        out.println(Utilities.getDocType());
        out.println("<HTML>" + Utilities.getHead("Administrador del Sistema"));
        out.println("<div id=\"container\">");
        out.println(Utilities.getIntro("Vista de Modificacion de Proyectos",root));
        
        String form = 
        	"<div id=\"pageContent\">"+
                        this.getForm(proyecto) +
                        Utilities.getFooter() + 
                "</div>";
        
        out.println(form);
        out.println(Utilities.getMenu());
        out.println("</div> <!-- fin container---></BODY></HTML>");
        out.close();
    }

    @Override
    public String getServletInfo() {
        return "Vista de Proyectos";
    }

    //Metodos para la forma de Crear
    public String getForm()
    {
        UserManager manager = new UserManager();
        String lideres="";
            for(LiderProyecto lider : manager.getLideres())
            {
                lideres +="<option value=\""+ lider.getId() +"\">"+
                            lider.getNombre() + " " + lider.getApellidoPaterno() + " " + lider.getApellidoMaterno()+
                          "</option>";
            }

        return 
            "<form action=\"ProjectControl\" method=\"GET\">" +
                "<div id=\"selection1\">" +
                    "<h3><span>Nombre del Proyecto</span></h3>" +
                    "<p class=\"p1\"><span>" + 
                        "<input type=\"text\" name=\"tbNombreProyecto\" size=\"20\">" +
                    "</span></p>" +
                "</div>" +
		"<div id=\"selection2\">" +
                    "<h3><span>Líder de Proyecto</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<select name=\"lider\">" +
                            lideres +
			"</select>" +
                    "</span></p> " +
		"</div> " +
		"<div id=\"selection3\"> " +
                    this.getFecha("Fecha de Inicio","i") +
                "</div>" +
		"<div id=\"selection4\">" +
                    this.getFecha("Fin de Proyecto","f") +
		"</div>" +
		"<div name=\"selection5\">" +
			"<p class=\"p1\"><span>" +
				"<input type=\"submit\" value=\"Crear\" name=\"CA\">" +
				"<input type=\"reset\" value=\"Cancelar\" name=\"CC\">" +
			"</span></p>" +
		"</div>" +
            "</form>";
    }
    //Metodos para la forma de Crear
    public String getFecha(String title, String value)
    {
        Calendar calendar = Calendar.getInstance();
        String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        int month = calendar.get(Calendar.MONTH);
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        return Utilities.getFormFecha(title, value, day,month,year);
    }
    
    //Metodos para la forma de modificar
    public String getForm(String proyecto)
    {
        ProjectManager manager = new ProjectManager();
        Proyecto project = manager.getProyecto(proyecto);
        project.getNombre();
        
        UserManager userManager = new UserManager();
        String lideres="";
            for(LiderProyecto lider : userManager.getLideres())
            {
              lideres +="<option"+ ((project.getLider()!= null && project.getLider().equals(lider))?" selected ":" ") +
                          "value=\""+ lider.getId() +"\">"+
                            lider.getNombre() + " " + lider.getApellidoPaterno() + " " + lider.getApellidoMaterno()+
                        "</option>";
            }

        return 
            "<form action=\"ProjectControl\" method=\"GET\">" +
                "<div id=\"selection1\">" +
                    "<h3><span>Id</span></h3>" +
                    "<p class=\"p1\"><span>" + 
                        "<input readonly type=\"text\" name=\"id\" size=\"20\"" +
                            " value=\"" + project.getId() + "\">" +
                        "</input>" +
                    "</span></p>" +
                    "<h3><span>Nombre del Proyecto</span></h3>" +
                    "<p class=\"p1\"><span>" + 
                        "<input type=\"text\" name=\"tbNombreProyecto\" size=\"20\"" +
                            " value=\"" + project.getNombre() + "\">" +
                        "</input>" +
                    "</span></p>" +
                "</div>" +
		"<div id=\"selection2\">" +
                    "<h3><span>Líder de Proyecto</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<select name=\"lider\">" +
                            lideres +
			"</select>" +
                    "</span></p> " +
		"</div> " +
		"<div id=\"selection3\"> " +
                    this.getFecha("Fecha de Inicio","i",project) +
                "</div>" +
		"<div id=\"selection4\">" +
                    this.getFecha("Fin de Proyecto","f",project) +
		"</div>" +
		"<div name=\"selection5\">" +
			"<p class=\"p1\"><span>" +
				"<input type=\"submit\" value=\"Aceptar\" name=\"MA\">" +
				"<input type=\"reset\" value=\"Cancelar\" name=\"MC\">" +
			"</span></p>" +
		"</div>" +
            "</form>";
    }
    
    //Metodos para la forma de modificar
    public String getFecha(String title, String value, Proyecto project)
    {
        String day = (value.compareTo("f") == 0)?String.valueOf(project.getFechafin().getDate()):String.valueOf(project.getFechainicio().getDate());
        int month = (value.compareTo("f") == 0)?project.getFechafin().getMonth():project.getFechainicio().getMonth();
        String year = (value.compareTo("f") == 0)?String.valueOf(project.getFechafin().getYear()):String.valueOf(project.getFechainicio().getYear());
               year = String.valueOf(Integer.valueOf(year) + 1900);
        return Utilities.getFormFecha(title,value,day,month,year);
    }
}
